// pages/forum/forum.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputValue: "",
    tagsDate: [{
        "topic": "甄嬛传",
        "index": "1"
      },
      {
        "topic": "电视剧",
        "index": "2"
      },
      {
        "topic": "电影",
        "index": "3"
      },
      {
        "topic": "动漫",
        "index": "4"
      },
      {
        "topic": "原创剧本",
        "index": "5"
      },
      {
        "topic": "狂飙",
        "index": "6"
      }
    ],
    // 论坛数据列表
    list: [],
    // 颜色随机
    colorList: ['#FEAA33', '#B886F8', '#60A1F9', '#FE6769', '#8492FF'],
  },

  // 搜索框
  setInputValue: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  printInputValue: function () {
    console.log(this.data.inputValue)
    // 向后端发送请求，查询返回结果
    wx.request({
      url: `http://localhost:1234/forum/search?info=${this.data.inputValue}`,
      success: res => {
        console.log(res.data)
        this.setData({
          list: res.data
        })
      }
    })
  },
  // 跳转到作品详情页
  toDetail: function (e) {
    // console.log(e.currentTarget.dataset.id)
    var id = e.currentTarget.dataset.id;
    // console.log(id)
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + id,
    })
  },
  // 搜索
  handleSearch: function (e) {
    console.log(e.target.dataset.topic)
    let topic = e.target.dataset.topic;
    this.setData({
      inputValue: topic
    })
    wx.request({
      url: `http://localhost:1234/forum/search?info=${this.data.inputValue}`,
      success: res => {
        console.log(res.data)
        this.setData({
          list: res.data
        })
      }
    })
  },
  // 收藏
  handleColleat: function (e) {
    // console.log('collect' + e.currentTarget.dataset.id)
    // 收藏某个作品
    let workid = e.currentTarget.dataset.id;
    let userid = wx.getStorageSync('userid')
    // console.log("userid:" + userid)
    wx.request({
      url: `http://localhost:1234/forum/collect?workid=${workid}&userid=${userid}`,
      success: res => {
        console.log(res.data)
        if (res.data == true) {
          // console.log('userid为' + userid + '的用户收藏workid为' + workid + "的作品，成功！")
          this.getList();
        }
      }
    })
  },
  // 点赞
  handleLike: function (e) {
    // console.log('like' + e.currentTarget.dataset.id)
    // 点赞某个作品
    let workid = e.currentTarget.dataset.id;
    let userid = wx.getStorageSync('userid');
    wx.request({
      url: `http://localhost:1234/forum/like?workid=${workid}&userid=${userid}`,
      success: res => {
        console.log(res.data)
        if (res.data == true) {
          // console.log('userid为' + userid + '的用户点赞workid为' + workid + "的作品，成功！")
          this.getList();
        }
      }
    })
  },
  getList: function () {
    wx.request({
      url: 'http://localhost:1234/forum/getworklist',
      success: res => {
        console.log(res.data)
        this.setData({
          list: res.data
        })
      }
    })
  },
  // 跳转到发表页面
  goToPublish: function () {
    wx.navigateTo({
      url: '/pages/publish/publish'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let userid = wx.getStorageSync('userid');
    wx.request({
      url: `http://localhost:1234/forum?userid=${userid}`,
      success: res => {
        // console.log(res.data)
      }
    })
    wx.request({
      url: 'http://localhost:1234/forum/getworklist',
      success: res => {
        console.log(res.data)
        this.setData({
          list: res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})